package com.example.chirdrenserver.Service.UserLoginService.Impl;


/**
 * @Author: Yu
 * @CreateTime: 2025-06-27
 * @Description:
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.chirdrenserver.Constant.RedisConstant;
import com.example.chirdrenserver.Pojo.dto.EmailDTO;
import com.example.chirdrenserver.Pojo.dto.RegisterDTO;
import com.example.chirdrenserver.Pojo.dto.UserLoginDTO;
import com.example.chirdrenserver.Pojo.entity.Captcha;
import com.example.chirdrenserver.Pojo.entity.User;
import com.example.chirdrenserver.Service.CaptchaService;
import com.example.chirdrenserver.Service.EmailService;
import com.example.chirdrenserver.Service.UserLoginService.UserLoginSerivce;
import com.example.chirdrenserver.Service.UserRegisterService;
import com.example.chirdrenserver.Service.UserService.UserService;
import com.example.chirdrenserver.Utils.RedisCacheUtil;
import com.example.chirdrenserver.exception.BaseException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class UserLoginSerivceImpl implements UserLoginSerivce {
    @Autowired
    UserService userService;
    @Autowired
    CaptchaService captchaService;
    @Autowired
    RedisCacheUtil redisCacheUtil;
    @Autowired
    UserRegisterService userRegisterService;
    @Autowired
    EmailService emailService;

    @Override public User userLogin(UserLoginDTO userLoginDTO) {
        String bePassword = userLoginDTO.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(bePassword.getBytes());
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, userLoginDTO.getEmail()));
        if (ObjectUtils.isEmpty(user)) {
            throw new BaseException("没有该账号");
        } else if (!md5Password.equals(user.getPassword())) {
            throw new BaseException("密码错误");
        }

        return user;
    }


    @Override
    @Async
    public boolean validate(EmailDTO emailDTO) {
        String email = emailDTO.getEmail();
        final String code1 = emailDTO.getCode();
        Captcha captcha = captchaService.getCaptcha(emailDTO.getUuid());
        if (captcha == null) throw new BaseException("uuId为空");

        if (!captcha.getCode().equals(code1)) {
            throw new BaseException("code错误");
        }
        if (LocalDateTime.now().isAfter(LocalDateTime.parse(captcha.getExpireTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))) {
            throw new BaseException("uuid过期");
        }
        if (!code1.equals(captcha.getCode())) {
            return false;
        }

        String code = getSixCode();
        redisCacheUtil.set(RedisConstant.EMAIL_CODE + email, code, RedisConstant.EMAIL_CODE_TIME);
        emailService.send(email, "注册验证码:" + code + ",验证码5分钟之内有效");
        return true;
    }

    @Override
    public boolean register(RegisterDTO registerDTO) {


        // 注册成功后删除图形验证码
        if (userRegisterService.register(registerDTO)) {
            captchaService.deleteCaptcha(registerDTO.getUuid());
            return true;
        }
        return false;
    }

    /**
     * 生成6位邮箱验证码
     *
     * @return
     */
    public static String getSixCode() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int code = (int) (Math.random() * 10);
            builder.append(code);
        }
        return builder.toString();
    }
}
